package whf.framework.report.export.util;

import java.io.File;
import java.util.Collection;
import java.util.Properties;

import javax.servlet.ServletRequest;

import whf.framework.datasource.DataRow;
import whf.framework.util.StringUtils;
import whf.framework.web.tag.TableModel;

/**
 * @author king
 * @modify Dec 20, 2007 9:30:31 PM
 */
public class ExportUtils {
	public static String EXPORT_FILE_NAME = "en_";

	public static String EXPORT_TYPE = "et_";

	public static String EXPORT_HEADER_TITLE = "eht_";

	/**
	 * 生成好的待导出的报表内容
	 * @property String:EXPORT_DATA
	 */
	public final static String EXPORT_DATA = "EXPORT_DATA";
	
	/**
	 * true:导出报表
	 * @property String:EXPORT
	 */
	public final static String EXPORT = "e_";

	public final static String MIMETYPES_PROPERTIES = "mimeTypes.properties";

	public final static String PREFIX_CLASS_NAME = "whf.framework.report.export.";

	public final static String CLASS_NAME = "className";

	public final static String REPORT_NAME = "reportName";

	//导出类型
	public final static String PDF = "pdf";

	public final static String XLS = "xls";

	public final static String TXT = "txt";

	public final static String CSV = "csv";

	public final static String HTML = "html";

	public final static String APPLET = "applet";

	private static Properties properties;
	
	/**
	 * 导出的配置路径
	 * 
	 * @property String:EXPORT_VIEW
	 */
	public final static String EXPORT_VIEW = "report.export.view.";
	
	/**
	 * 导出路径
	 * @property String:EXPORT_FILES
	 */
	public final static String EXPORT_FILES = "report.export.files";
	
	/**
	 * 导出pdf时的标题字体路径
	 * @property String:EXPORT_PDF_TITLE_FONT
	 */
	public final static String EXPORT_PDF_TITLE_FONT = "report.export.pdf.title-font";
	
	/**
	 * 导出pdf时的内容字体路径
	 * @property String:EXPORT_PDF_BODY_FONT
	 */
	public final static String EXPORT_PDF_BODY_FONT = "report.export.pdf.body-font";
	
	/**
	 * 使用ThreadLocal变量存储数据
	 * @property ThreadLocal<TableModel>:tableModel
	 */
	private static ThreadLocal<TableModel> tableModel = new ThreadLocal<TableModel>();
	
	/**
	 * 定义报表返回
	 */
	private static ThreadLocal<Object> reportResult = new ThreadLocal<Object>();

	public static void setReportResult(Object obj) {
		reportResult.set(obj);
	}
	
	public static Object getReportResult() {
		return reportResult.get();
	}
	
	public static void setTableModel(TableModel table){
		tableModel.set(table);
	}
	
	public static TableModel getTableModel(){
		return tableModel.get();
	}
	
	public static Collection<DataRow> getDataSource(){
		return getTableModel().getDataSource().getDataSource();
	}
	
	public static boolean isExportMode(ServletRequest request) {
		return "true".equalsIgnoreCase(request.getParameter(EXPORT));
	}

	private static void init() {
		if (properties != null) {
			return;
		}
		try {
			properties = new Properties();
			properties.load(new ExportUtils().getClass().getResourceAsStream(MIMETYPES_PROPERTIES));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getFileMimeType(File file) {
		if (file == null) {
			return null;
		}
		return getFileMimeType(file.getName());
	}

	public static String getFileMimeType(String fileName) {
		if (StringUtils.isEmpty(fileName) || (fileName.indexOf(".") == -1)) {
			return null;
		}
		fileName = fileName.substring(fileName.lastIndexOf("."));
		return getExtensionMimeType(fileName);
	}

	public static String getExtensionMimeType(String extension) {
		String result = null;
		if (StringUtils.isEmpty(extension)) {
			return result;
		}
		init();
		extension = extension.toLowerCase();
		if (!extension.startsWith(".")) {
			extension = "." + extension;
		}
		result = (String) properties.get(extension);
		return result;
	}

	public static String parseXLS(String value) {
		if (StringUtils.isEmpty(value))
			return "";
		value = replaceNonBreakingSpaces(value);
		return value;
	}

	public static String parsePDF(String value) {
		if (StringUtils.isEmpty(value))
			return "";
		value = replaceNonBreakingSpaces(value);
		value = escapeChars(value);
		return value;
	}

	public static String parseCSV(String value) {
		if (StringUtils.isEmpty(value))
			return "";
		value = replaceNonBreakingSpaces(value);
		return value;
	}

	public static String replaceNonBreakingSpaces(String value) {
		if (StringUtils.isEmpty(value))
			return "";
		if (value.indexOf("&nbsp;") >= 0) {
			value = StringUtils.replace(value, "&nbsp;", "");
		}
		return value;
	}

	public static String getClassConversionName(String tableName) {
		StringBuffer sb = new StringBuffer(tableName.replaceAll("_", ""));
		if (!Character.isUpperCase(sb.charAt(0))) {
			char firstChar = Character.toUpperCase(sb.charAt(0));
			sb.deleteCharAt(0);
			sb.insert(0, firstChar);
		}
		return sb.toString();
	}

	public static String escapeChars(String value) {
		if (StringUtils.isEmpty(value))
			return "";
		if (value.indexOf("&")>=0) {
			value = StringUtils.replace(value, "&", "&#38;");
		}
		if (value.indexOf(">")>=0) {
			value = StringUtils.replace(value, ">", "&gt;");
		}
		if (value.indexOf("<")>=0) {
			value = StringUtils.replace(value, "<", "&lt;");
		}
		return value;
	}
}
